package com.edu.edu.day9_19_week7_homework.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.edu.edu.day9_19_week7_homework.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 9/19 0019.
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText userName;
    private EditText passWord;
    private Button bt_login;
    private BmobUser mUser;
    private Button bt_findPassWord;
    private Button bt_register;
    private String userName1;
    private String passWord1;
    private TextInputLayout input_login;
    private TextInputLayout input_register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);//绑定登录页面布局
        //第一：默认初始化
        Bmob.initialize(LoginActivity.this, "fe5f0d457e1c81e0286d8f68e74a603d");
        initView();//初始化控件
    }

    private void initView() {
        userName = (EditText) findViewById(R.id.login_userName);
        passWord = (EditText) findViewById(R.id.login_userPassword);
        bt_login = (Button) findViewById(R.id.login_login);
        bt_register = (Button) findViewById(R.id.login_register);
        bt_findPassWord = (Button) findViewById(R.id.login_findPassWord);
        input_login = (TextInputLayout) findViewById(R.id.input_login);
        input_register = (TextInputLayout) findViewById(R.id.input_register);
        //设置按钮监听
        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
        bt_findPassWord.setOnClickListener(this);
        // BmobUser类-->使用BmobSDK提供的注册功能
        mUser = new BmobUser();
    }

    @Override
    public void onClick(View view) {
        //获取用户名跟密码
        Intent intent = getIntent();
        String phoneTel = intent.getStringExtra("phoneTel");
        String password = intent.getStringExtra("password");
        userName1 = this.userName.getText().toString().toString();
        passWord1 = this.passWord.getText().toString().toString();
        switch (view.getId()){
            case R.id.login_login://登录成-->跳转到主界面
                //判断用户名跟密码是否为空
                if(TextUtils.isEmpty(userName1) || TextUtils.isEmpty(passWord1)){
                    Toast.makeText(LoginActivity.this,"用户名跟密码不能为空!",Toast.LENGTH_LONG).show();
                    return;
                }//判断用户名跟密码是否正确
                else if(userName1.equals(phoneTel) && passWord1.equals(password)){
                    input_login.setErrorEnabled(true);
                    input_register.setErrorEnabled(true);
                    input_login.setError("用户名正确");
                    input_register.setError("密码正确");
                }
//
                //验证用户名跟密码
                mUser.setUsername(userName1);
                mUser.setPassword(passWord1);
                //使用bmob的login方法进行登录
                mUser.login(LoginActivity.this, new SaveListener() {
                    @Override
                    public void onSuccess() {
                        //登录成功跳转到主界面
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        //将电话号码传过去
                        String str = "注销";
                        intent.putExtra("phone",userName1);
                        intent.putExtra("str",str);
                        startActivity(intent);
                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                        finish();//登录成功之后销毁主界面
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(LoginActivity.this,"登录失败,请检查网络！",Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.login_register://注册账号--->进入到注册页面
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            case R.id.login_findPassWord://找回密码
                Toast.makeText(LoginActivity.this,"当前未登录，请先进行注册！",Toast.LENGTH_LONG).show();
                break;
        }
    }
}
